package com.example.web.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.web.SysConst;
import com.example.web.dto.*;
import com.example.web.dto.query.*;
import com.example.web.entity.*;
import com.example.web.mapper.*;
import com.example.web.service.*;
import com.example.web.tools.dto.*;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import lombok.SneakyThrows;
import com.example.web.tools.*;
/**
 * 户籍个人信息功能实现类
 */
@Service
public class CensusInfoPersonServiceImpl extends ServiceImpl<CensusInfoPersonMapper, CensusInfoPerson> implements CensusInfoPersonService {

	 /**
     * 操作数据库AppUser表mapper对象
     */
    @Autowired
    private AppUserMapper _AppUserMapper;
    /**
     * 操作数据库的CensusInfoPerson表mapper对象
     */
    @Autowired
    private CensusInfoPersonMapper _CensusInfoPersonMpper;

 			
    @Autowired
    private HouseholdMapper _HouseholdMapper;                        
			
 			
			

    /**
     * 户籍个人信息分页查询
     */
    @SneakyThrows
    @Override
    public PagedResult<CensusInfoPersonDto> List(CensusInfoPersonPagedInput input) {

      
        //声明一个支持户籍个人信息查询的(拉姆达)表达式
        LambdaQueryWrapper<CensusInfoPerson> queryWrapper = Wrappers.<CensusInfoPerson>lambdaQuery()
                .eq(input.getId()!=null,CensusInfoPerson::getId,input.getId())
                .eq(input.getCreatorId()!=null,CensusInfoPerson::getCreatorId,input.getCreatorId());
      
     	 //如果前端搜索传入RelativeUserId不为空,则进行精确查询
        if(input.getRelativeUserId()!=null) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getRelativeUserId,input.getRelativeUserId());
       	 }
     	 //如果前端搜索传入Nationality不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getNationality())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getNationality,input.getNationality());
       	 }
     	 //如果前端搜索传入Photo不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getPhoto())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getPhoto,input.getPhoto());
       	 }
     	 //如果前端搜索传入IdCard不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getIdCard())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getIdCard,input.getIdCard());
       	 }
     	 //如果前端搜索传入Name不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getName())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getName,input.getName());
       	 }
     	 //如果前端搜索传入Educational不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getEducational())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getEducational,input.getEducational());
       	 }
     	 //如果前端搜索传入Sex不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getSex())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getSex,input.getSex());
       	 }
     	 //如果前端传入了Birth时间范围,则进行时间范围搜索
        if(input.getBirthRange()!=null&&input.getBirthRange().stream().count()>1)
        {
            queryWrapper=queryWrapper.lt(CensusInfoPerson::getBirth,input.getBirthRange().get(1));
            queryWrapper=queryWrapper.gt(CensusInfoPerson::getBirth,input.getBirthRange().get(0));
        }
     	 //如果前端搜索传入Marriage不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getMarriage())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getMarriage,input.getMarriage());
       	 }
     	 //如果前端搜索传入HouseholdId不为空,则进行精确查询
        if(input.getHouseholdId()!=null) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getHouseholdId,input.getHouseholdId());
       	 }
     	 //如果前端搜索传入Occupation不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getOccupation())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getOccupation,input.getOccupation());
       	 }
     	 //如果前端搜索传入OriginAddress不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getOriginAddress())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getOriginAddress,input.getOriginAddress());
       	 }
     	 //如果前端搜索传入Hometown不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getHometown())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getHometown,input.getHometown());
       	 }
     	 //如果前端搜索传入Relative不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getRelative())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getRelative,input.getRelative());
       	 }
     	 //如果前端搜索传入BirthAddress不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getBirthAddress())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getBirthAddress,input.getBirthAddress());
       	 }
     	 //如果前端搜索传入MilitaryStatus不为空,则进行精确查询
        if(input.getMilitaryStatus()!=null) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getMilitaryStatus,input.getMilitaryStatus());
       	 }
     	 //如果前端搜索传入BeforeName不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getBeforeName())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getBeforeName,input.getBeforeName());
       	 }
     	 //如果前端搜索传入OtherAddress不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getOtherAddress())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getOtherAddress,input.getOtherAddress());
       	 }
     	 //如果前端搜索传入ReligiousBelief不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getReligiousBelief())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getReligiousBelief,input.getReligiousBelief());
       	 }
     	 //如果前端搜索传入HeightStartRange,则要大于HeightStartRange输入的值
         if(input.getHeightStartRange()!=null)
          {
            queryWrapper=queryWrapper.gt(CensusInfoPerson::getHeight,input.getHeightStartRange());
          }
         //如果前端搜索传入HeightEndRange,则要小于HeightEndRange输入的值
      		 if(input.getHeightEndRange()!=null)
          {
            queryWrapper=queryWrapper.lt(CensusInfoPerson::getHeight,input.getHeightEndRange());
          }
     	 //如果前端搜索传入BloodType不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getBloodType())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getBloodType,input.getBloodType());
       	 }
     	 //如果前端搜索传入RegisteredAddress不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getRegisteredAddress())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getRegisteredAddress,input.getRegisteredAddress());
       	 }
     	 //如果前端搜索传入WhenWhereMoveCity不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getWhenWhereMoveCity())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getWhenWhereMoveCity,input.getWhenWhereMoveCity());
       	 }
     	 //如果前端搜索传入WhenWhereMoveAddress不为空,则进行精确查询
        if(Extension.isNotNullOrEmpty(input.getWhenWhereMoveAddress())) {
            queryWrapper=queryWrapper.eq(CensusInfoPerson::getWhenWhereMoveAddress,input.getWhenWhereMoveAddress());
       	 }
      
        //按创建时间从大到小排序 最新的显示在最前面
        queryWrapper=queryWrapper.orderByDesc(CensusInfoPerson::getCreationTime);
        //构建一个分页查询的model
        Page<CensusInfoPerson> page = new Page<>(input.getPage(), input.getLimit());
          
   
         //从数据库进行分页查询获取户籍个人信息数据
        IPage<CensusInfoPerson> pageRecords= _CensusInfoPersonMpper.selectPage(page, queryWrapper);

        //获取所有满足条件的数据行数
        Long totalCount= _CensusInfoPersonMpper.selectCount(queryWrapper);

        //把CensusInfoPerson实体转换成CensusInfoPerson传输模型
        List<CensusInfoPersonDto> items= Extension.copyBeanList(pageRecords.getRecords(),CensusInfoPersonDto.class);
        for (CensusInfoPersonDto item : items) {
          
          //查询出关联的创建用户信息
            AppUserDto  CreatorAppUserDTO=new AppUserDto();
            AppUser  CreatorAppUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getCreatorId()));
           if(CreatorAppUserEntity!=null) {
                BeanUtils.copyProperties(CreatorAppUserDTO, CreatorAppUserEntity);
                item.setCreatorAppUserDto(CreatorAppUserDTO);
            }  
         
           
          	            
           //查询出关联的Household表信息
           HouseholdDto HouseholdDTO = new HouseholdDto();        
            Household  HouseholdEntity= _HouseholdMapper.selectOne(Wrappers.<Household>lambdaQuery().eq(Household::getId,item.getHouseholdId()));
           if(HouseholdEntity!=null) {  
          BeanUtils.copyProperties(HouseholdDTO,HouseholdEntity);
            item.setHouseholdDto(HouseholdDTO); 
           }
          
           
          	            
           //查询出关联的AppUser表信息
           AppUserDto RelativeUserDTO = new AppUserDto();        
            AppUser  RelativeUserEntity= _AppUserMapper.selectOne(Wrappers.<AppUser>lambdaQuery().eq(AppUser::getId,item.getRelativeUserId()));
           if(RelativeUserEntity!=null) {  
          BeanUtils.copyProperties(RelativeUserDTO,RelativeUserEntity);
            item.setRelativeUserDto(RelativeUserDTO); 
           }
          
        }
      
        //返回一个分页结构给前端
        return PagedResult.GetInstance(items,totalCount);

    }
  
    /**
     * 单个户籍个人信息查询
     */
    @SneakyThrows
    @Override
    public CensusInfoPersonDto Get(CensusInfoPersonPagedInput input) {
       if(input.getId()==null)
        {
            return new CensusInfoPersonDto();
        }
        PagedResult<CensusInfoPersonDto>  pagedResult =List(input);
        return pagedResult.getTotalCount()>0?pagedResult.getItems().stream().findFirst().get():new CensusInfoPersonDto();
    }

    /**
     *户籍个人信息创建或者修改
     */
    @SneakyThrows
    @Override
    public CensusInfoPersonDto CreateOrEdit(CensusInfoPersonDto input) {

        //声明一个户籍个人信息实体
        CensusInfoPerson CensusInfoPerson = new CensusInfoPerson();

        //把前端传入的input参数拷贝到户籍个人信息实体
        BeanUtils.copyProperties(CensusInfoPerson,input);

        //调用数据库的增加或者修改方法
        saveOrUpdate(CensusInfoPerson);

        //定义一个返回给前端的户籍个人信息传输模型
        CensusInfoPersonDto CensusInfoPersonDto=new CensusInfoPersonDto();

        //同理把操作的户籍个人信息实体拷贝给户籍个人信息传输模型
        BeanUtils.copyProperties(CensusInfoPersonDto,CensusInfoPerson);

        //把传输模型返回给前端
        return CensusInfoPersonDto;
    }



}
